package com.eb.controller;

import com.eb.pojo.BargainGoods;
import com.eb.pojo.Result;
import com.eb.service.KjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.PageUtil;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("kj")
@CrossOrigin
public class KjController {

    @Autowired
    private KjService kjService;


    @GetMapping
    public PageUtil<Map<String,Object>> list(@RequestParam(value = "name",defaultValue = "",required = false)String name,
                                             @RequestParam(value = "page",defaultValue = "1",required = false)int page,
                                             @RequestParam(value = "size",defaultValue = "10",required = false)int size){
        Page<Map<String,Object>> list=kjService.list(name,page,size);
        return new PageUtil<Map<String,Object>>(page,list.getTotalElements(),list.getContent(),size);
    }
    @PostMapping
    public Result addKjShop(@RequestBody BargainGoods bargainGoods){
        try {
            kjService.add(bargainGoods);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }
    @DeleteMapping("/{id}")
    public Result deleteShop(@PathVariable("id")long id){
        try {
            kjService.delete(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

}
